// 内容模块API

import api from '../index';
import {
  ApiResponse,
  CreateContentRequest,
  UpdateContentTextRequest,
  UpdateCoverRequest,
  UpdateVideoRequest,
  UpdateImagesRequest,
  ContentQueryParams,
  OperationLogQueryParams,
  Content,
  ContentDetail,
  ContentMedia,
  ContentListResponse,
  OperationLogListResponse,
} from '../../types';

// 1. 内容创建

// 创建内容
export const createContent = async (
  data: CreateContentRequest
): Promise<ApiResponse<Content>> => {
  const formData = new FormData();
  
  formData.append('contentType', data.contentType);
  formData.append('title', data.title);
  formData.append('textContent', data.textContent);
  
  if (data.cover) {
    formData.append('cover', data.cover);
  }
  
  if (data.images && data.images.length > 0) {
    data.images.forEach((image) => {
      formData.append('images', image);
    });
  }
  
  if (data.video) {
    formData.append('video', data.video);
  }

  const response = await api.post('/contents', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
  
  return response.data;
};

// 2. 内容更新

// 更新标题和文本内容
export const updateContentText = async (
  contentId: number,
  data: UpdateContentTextRequest
): Promise<ApiResponse<null>> => {
  const response = await api.put(`/contents/${contentId}/text`, data);
  return response.data;
};

// 更新封面
export const updateCover = async (
  contentId: number,
  data: UpdateCoverRequest
): Promise<ApiResponse<null>> => {
  const formData = new FormData();
  formData.append('cover', data.cover);

  const response = await api.put(`/contents/${contentId}/cover`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
  
  return response.data;
};

// 更新视频
export const updateVideo = async (
  contentId: number,
  data: UpdateVideoRequest
): Promise<ApiResponse<null>> => {
  const formData = new FormData();
  formData.append('video', data.video);

  const response = await api.put(`/contents/${contentId}/video`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
  
  return response.data;
};

// 批量更新图片
export const updateImages = async (
  contentId: number,
  data: UpdateImagesRequest
): Promise<ApiResponse<null>> => {
  const formData = new FormData();
  
  if (data.deleteImageIds) {
    formData.append('deleteImageIds', data.deleteImageIds);
  }
  
  if (data.newImages && data.newImages.length > 0) {
    data.newImages.forEach((image) => {
      formData.append('newImages', image);
    });
  }

  const response = await api.put(`/contents/${contentId}/images`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
  
  return response.data;
};

// 3. 内容删除

// 删除内容（软删除）
export const deleteContent = async (
  contentId: number
): Promise<ApiResponse<null>> => {
  const response = await api.delete(`/contents/${contentId}`);
  return response.data;
};

// 4. 媒体文件查询

// 获取内容媒体文件
export const getContentMedia = async (
  contentId: number
): Promise<ApiResponse<ContentMedia>> => {
  const response = await api.get(`/contents/${contentId}/media`);
  return response.data;
};

// 5. 内容列表查询

// 获取用户自己的内容列表
export const getMyContents = async (
  params: ContentQueryParams = {}
): Promise<ApiResponse<ContentListResponse>> => {
  const queryParams = new URLSearchParams();
  
  if (params.page) queryParams.append('page', params.page.toString());
  if (params.size) queryParams.append('size', params.size.toString());
  if (params.title && params.title !== 'all') queryParams.append('title', params.title);
  if (params.textContent && params.textContent !== 'all') queryParams.append('textContent', params.textContent);
  if (params.contentType && params.contentType !== 'all') queryParams.append('contentType', params.contentType);
  if (params.orderBy) queryParams.append('orderBy', params.orderBy);

  const response = await api.get(`/contents/my?${queryParams.toString()}`);
  return response.data;
};

// 获取公开内容列表
export const getPublicContents = async (
  params: ContentQueryParams = {}
): Promise<ApiResponse<ContentListResponse>> => {
  const queryParams = new URLSearchParams();
  
  if (params.page) queryParams.append('page', params.page.toString());
  if (params.size) queryParams.append('size', params.size.toString());
  if (params.title && params.title !== 'all') queryParams.append('title', params.title);
  if (params.textContent && params.textContent !== 'all') queryParams.append('textContent', params.textContent);
  if (params.contentType && params.contentType !== 'all') queryParams.append('contentType', params.contentType);
  if (params.orderBy) queryParams.append('orderBy', params.orderBy);
  if (params.userId) queryParams.append('userId', params.userId.toString());

  const response = await api.get(`/contents/public?${queryParams.toString()}`);
  return response.data;
};

// 获取所有内容列表（管理员）
export const getAllContents = async (
  params: ContentQueryParams = {}
): Promise<ApiResponse<ContentListResponse>> => {
  const queryParams = new URLSearchParams();
  
  if (params.page) queryParams.append('page', params.page.toString());
  if (params.size) queryParams.append('size', params.size.toString());
  if (params.title && params.title !== 'all') queryParams.append('title', params.title);
  if (params.textContent && params.textContent !== 'all') queryParams.append('textContent', params.textContent);
  if (params.contentType && params.contentType !== 'all') queryParams.append('contentType', params.contentType);
  if (params.orderBy) queryParams.append('orderBy', params.orderBy);
  if (params.userId) queryParams.append('userId', params.userId.toString());

  const response = await api.get(`/contents/admin?${queryParams.toString()}`);
  return response.data;
};

// 浏览内容详情
export const viewContent = async (
  contentId: number
): Promise<ApiResponse<ContentDetail>> => {
  const response = await api.get(`/contents/${contentId}/view`);
  return response.data;
};

// 6. 操作日志查询

// 获取操作日志历史
export const getOperationLogs = async (
  params: OperationLogQueryParams = {}
): Promise<ApiResponse<OperationLogListResponse>> => {
  const queryParams = new URLSearchParams();
  
  if (params.page) queryParams.append('page', params.page.toString());
  if (params.size) queryParams.append('size', params.size.toString());
  if (params.operationType && params.operationType !== 'all') queryParams.append('operationType', params.operationType);
  if (params.contentId) queryParams.append('contentId', params.contentId.toString());
  if (params.userId) queryParams.append('userId', params.userId.toString());

  const response = await api.get(`/contents/operation-logs?${queryParams.toString()}`);
  return response.data;
}; 